package com.sky.business.sys.handler.cmcc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.domain.SystemConfig;
import com.sky.business.sys.event.AdJustDeviceInfoEvent;
import com.sky.business.sys.event.AdJustDeviceInfoReportEvent;
import com.sky.business.sys.handler.MqttEventHandler;
import com.sky.business.sys.service.CameraConfigService;
import com.sky.business.sys.service.SystemConfigService;
import com.sky.business.sys.util.MQTTClientUtils;
import com.sky.common.config.CacheConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.SimpleTimeZone;

/**
 * 移动凌云平台开始跟踪任务处理器
 *
 */
@Component
@Slf4j
public class AdjustDeviceInfoEventHandler implements MqttEventHandler {

    public static ObjectMapper objectMapper = new ObjectMapper().setTimeZone(SimpleTimeZone.getTimeZone("GMT+8"));

    @Autowired
    private CameraConfigService cameraConfigService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Override
    public boolean match(String topic, String msg,String event) {
        return "AdjustDeviceInfo".equals(event);
    }

    @Override
    public void handle(String topic, String msg) throws Exception {
        SystemConfig systemConfig = systemConfigService.getSystemConfig();
        AdJustDeviceInfoEvent adJustDeviceInfoEvent = objectMapper.readValue(msg, AdJustDeviceInfoEvent.class);
        AdJustDeviceInfoEvent.Metadata metadata = adJustDeviceInfoEvent.getMetadata();
        CameraConfig cameraConfig = cameraConfigService.findByDeviceId(metadata.getDeviceId());
        if (cameraConfig == null) {
            log.error("设备不存在,deviceId:{}", metadata.getDeviceId());
            //发送报警信息
            AdJustDeviceInfoReportEvent event = new AdJustDeviceInfoReportEvent(adJustDeviceInfoEvent.getEdgeId(), metadata.getDeviceId(), CacheConstants.CODE_STATUS_ERROR, "设备不存在", adJustDeviceInfoEvent.getMetadata().getDeviceInfo());
            String responseMsg = objectMapper.writeValueAsString(event);
            MQTTClientUtils.publish(MQTTClientUtils.CMCC_MQTT_CLIENT, systemConfig.getCmccReportingTopic(), CacheConstants.DEFAULT_QOS, responseMsg);
        } else {
            AdJustDeviceInfoEvent.DeviceInfo deviceInfo = adJustDeviceInfoEvent.getMetadata().getDeviceInfo();
            cameraConfig.setCameraLongitude(String.valueOf(deviceInfo.getLongitude()));
            cameraConfig.setCameraLatitude(String.valueOf(deviceInfo.getLatitude()));
            cameraConfig.setCameraAltitude(String.valueOf(deviceInfo.getAltitude()));
            cameraConfig.setPanAngle(String.valueOf(deviceInfo.getPanAngleTrim()));
            cameraConfig.setTiltAngle(String.valueOf(deviceInfo.getTiltAngleTrim()));
            cameraConfigService.updateById(cameraConfig);
            //转发
            MQTTClientUtils.publish(MQTTClientUtils.LOCAL_MQTT_CLIENT, cameraConfig.getDispatcherTopic(), CacheConstants.DEFAULT_QOS, msg);
        }
    }
}
